﻿using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Services;

namespace Contour.Contrib.WorkflowTypes
{
    /// <summary>
    /// NoSpam - Umbraco Contour Workflow
    /// Purpose: Determines if a hidden field changed from its default value and handles record accoring to property settings
    /// Author: jason.mitchell@interclick.com - 6/28/2011
    /// </summary>
    public class NoSpam : WorkflowType
    {
        #region Properties
        [Umbraco.Forms.Core.Attributes.Setting("Label Name",
            description = "The label named for the control",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string LabelName { get; set; }

        [Umbraco.Forms.Core.Attributes.Setting("Default Value",
            description = "Define the default value for the control",
            control = "Umbraco.Forms.Core.FieldSetting.TextField")]
        public string DefaultValue { get; set; }

        [Umbraco.Forms.Core.Attributes.Setting("Record Action",
            prevalues = "Delete Record, Approve Record, Do Nothing",
            description = "The action if record is marked as spam",
            control = "Umbraco.Forms.Core.FieldSetting.Dropdownlist")]
        public string RecordAction { get; set; }
        #endregion

        #region Constructor
        public NoSpam()
        {
            this.Id = new Guid("31804252-AD24-4755-B040-2DE0282D893C");
            this.Name = "Determines if record is spam";
            this.Description = "Changes the state of the record being processed";
        }
        #endregion

        #region Override Methods
        public override List<Exception> ValidateSettings()
        {
            List<Exception> exceptions = new List<Exception>();

            if (String.IsNullOrEmpty(this.LabelName))
                exceptions.Add(new Exception("Label of control not set"));

            if (String.IsNullOrEmpty(this.DefaultValue))
                exceptions.Add(new Exception("Default value of control is not set"));

            if (String.IsNullOrEmpty(this.RecordAction))
                exceptions.Add(new Exception("Record action not set"));

            return new List<Exception>();
        }

        public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
        {
            bool isSpam = false;

            // LINQ statement that returns the field based on the label name
            RecordField field = record.RecordFields.Values.Where(value => value.Field.Caption == LabelName).FirstOrDefault();

            // Checks to see if the field was found and if its value is still equal to the default value
            if (field == null || (field != null & !String.Equals(field.ValuesAsString(), this.DefaultValue, StringComparison.Ordinal)))
            {
                isSpam = true;
            }

            // Updates the record according to workflow properties
            using (RecordService rs = new RecordService(record))
            {
                if (isSpam)
                {
                    if (String.Equals(RecordAction, "Delete Record", StringComparison.OrdinalIgnoreCase))
                    {
                        rs.Delete();
                    }
                    else if (String.Equals(RecordAction, "Approve Record", StringComparison.OrdinalIgnoreCase))
                    {
                        rs.Approve();
                    }
                }
                else
                {
                    rs.Approve();
                }
            }

            return WorkflowExecutionStatus.Completed;
        }
        #endregion
    }
}